%-------------------------------------------------------------------------
% Subroutine: Figures showing shock contribution to housing boom
%
% Estimation Codes for State-Level Results in  
% Ben-David, Itzhak, Sebastian Weber, and Pascal Towbin 
% "Inferring Expectations from Observables: Evidence from the Housing Market" 
% Review of Economics and Statistics
% ------------------------------------------------------------------------ 

%% Box an Whisker Chart for States
nostat=size(StateLab,2);

% Show TOP 20 states
limit=20;               

% Define whisker properties
q3=norminv(.75);        
q84=norminv(0.84);
w84=(q84-q3)/(2*q3);

% Contribution from Expectation Shock
figure('Name','Figure 6a: Expectation Shock')
set(gcf, 'Name','Figure 6a: Expectation Shock','NumberTitle','off') 
yvarbar=mean(coll_speccontrib,1)';
m_yvarbar=mean(coll_speccontrib,1)';
[~,sortorder] = sort(yvarbar);
sort_StateLab=StateLab(sortorder');
boxplot(coll_speccontrib(:,sortorder(1+nostat-limit:end)'),sort_StateLab(1+nostat-limit:end),'symbol','','whisker',w84);
y1=[-10 35];
ylim(y1)
yline(0)
% Overlay the mean as green diamonds
hold on
plot(m_yvarbar(sortorder(1+nostat-limit:end)), 'dk')
hold off 

% Contribution from Housing Consumption Shock
figure('Name','Figure 6c: Housing Consumption Shock')
set(gcf, 'Name','Figure 6c: Housing Consumption Shock','NumberTitle','off') 
yvarbar=mean(coll_demandcontrib,1)';
m_yvarbar=mean(coll_demandcontrib,1)';
[~,sortorder] = sort(yvarbar);
sort_StateLab=StateLab(sortorder');
boxplot(coll_demandcontrib(:,sortorder(1+nostat-limit:end)'),sort_StateLab(1+nostat-limit:end),'symbol','','whisker',w84);
ylim(y1)
yline(0)
% Overlay the mean as green diamonds
hold on
plot(m_yvarbar(sortorder(1+nostat-limit:end)), 'dk')
hold off 

% Contribution from Housing Supply Shock
figure('Name','Figure 6d: Housing Supply Shock')
set(gcf, 'Name','Figure 6d: Housing Supply Shock','NumberTitle','off') 
yvarbar=mean(coll_supplycontrib,1)';
m_yvarbar=mean(coll_supplycontrib,1)';
[~,sortorder] = sort(yvarbar);
sort_StateLab=StateLab(sortorder');
boxplot(coll_supplycontrib(:,sortorder(1+nostat-limit:end)'),sort_StateLab(1+nostat-limit:end),'symbol','','whisker',w84);
ylim(y1)
yline(0)
% Overlay the mean as green diamonds
hold on
plot(m_yvarbar(sortorder(1+nostat-limit:end)), 'dk')
hold off 

% Contribution from Mortgage Rate Supply Shock
figure('Name','Figure 6b: Mortgage Rate Shock')
set(gcf, 'Name','Figure 6b: Mortgage Rate Shock','NumberTitle','off') 
yvarbar=mean(coll_intcontrib,1)';
m_yvarbar=mean(coll_intcontrib,1)';
[~,sortorder] = sort(yvarbar);
sort_StateLab=StateLab(sortorder');
boxplot(coll_intcontrib(:,sortorder(1+nostat-limit:end)'),sort_StateLab(1+nostat-limit:end),'symbol','','whisker',w84);
ylim(y1)
yline(0)
% Overlay the mean as green diamonds
hold on
plot(m_yvarbar(sortorder(1+nostat-limit:end)), 'dk')
hold off 


